/*
 * This file is part of CompetitionGenerator.
 * CompetitionGenerator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * CompetitionGenerator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * 
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with CompetitionGenerator. If not, see <http://www.gnu.org/licenses/>.
 */
package competitiongenerator;

import competitiongenerator.entities.Category;
import competitiongenerator.entities.CompetitionAreaKarate;
import competitiongenerator.entities.CompetitorKarate;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * TestEntities class.
 * @author Ashitaka
 */
public class TestEntities {

    /**
     * InputStreamReader.
     */
    private InputStreamReader isr;
    /**
     * BufferedReader.
     */
    private BufferedReader br;
    /**
     * Logger.
     */
    private static final Logger logger = Logger.getLogger(TestEntities.class.getName());

    /**
     * Constructor.
     */
    public TestEntities() {
        isr = new InputStreamReader(System.in);
        br = new BufferedReader(isr);
    }

    /**
     * Obtains the one of the actions given in the argument options.
     * @param options The valid options.
     * @return The choosen option.
     */
    public String getAction(String[] options) {
        String result = null;
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < options.length; i++) {
            sb.append(options[i]);
            if (i != options.length - 1) {
                sb.append("|");
            } else {
                sb.append("]: ");
            }
        }
        boolean exit = false;
        while (!exit) {
            try {
                System.out.print(sb.toString());
                String s = br.readLine();
                for (int j = 0; j < options.length; j++) {
                    if (s.equalsIgnoreCase(options[j])) {
                        result = options[j];
                        exit = true;
                        break;
                    }
                }
                if (!exit) {
                    System.out.println("Invalid parameters, try again.");
                }
            } catch (IOException ex) {
                logger.log(Level.SEVERE, "Error during parameters catch.", ex);
            }
        }
        return result;
    }

    public void run() {
        List<CompetitorKarate> competitorList = new ArrayList<CompetitorKarate>();
        List<Category> categoryList = new ArrayList<Category>();
        List<CompetitionAreaKarate> competitionArea = new ArrayList<CompetitionAreaKarate>();

        boolean exit = false;

        while (!exit) {
            try {
                String[] a = {"add", "set", "print", "bye"};
                String s = getAction(a);
                if (s.equalsIgnoreCase("add")) {
                    String[] b = {"competitor", "category", "tatami"};
                    s = getAction(b);
                    if (s.equalsIgnoreCase("competitor")) {
                        System.out.print("Competitor Name: ");
                        String name = br.readLine();
                        System.out.print("Competitor First Surname: ");
                        String fsurname = br.readLine();
                        System.out.print("Competitor Range: ");
                        String range = br.readLine();
                        CompetitorKarate comp = new CompetitorKarate(name, fsurname, CompetitorKarate.getCompRangeGrade(range));
                        competitorList.add(comp);
                        if (logger.isLoggable(Level.INFO)) {
                            logger.log(Level.INFO, "Competitor successfully added.");
                        }
                    } else if (s.equalsIgnoreCase("category")) {
                        exit = true;
                    } else if (s.equalsIgnoreCase("tatami")) {
                        System.out.print("Competition Area Name: ");
                        String name = br.readLine();
                        CompetitionAreaKarate compArea = new CompetitionAreaKarate(name);
                        competitionArea.add(compArea);
                        if (logger.isLoggable(Level.INFO)) {
                            logger.log(Level.INFO, "Competition Area successfully added.");
                        }
                    } else {
                        if (logger.isLoggable(Level.WARNING)) {
                            logger.log(Level.WARNING, "No command found (Level 2).");
                        }
                    }
                } else if (s.equalsIgnoreCase("set")) {
                    boolean valid = false;
                    while (!valid) {
                        System.out.print("[competitor|area]");
                        s = br.readLine();
                        if (s.equalsIgnoreCase("competitor")) {
                            System.out.print("Which competitor do you want to edit attributes to? ");
                            s = br.readLine();
                            for (CompetitorKarate c : competitorList) {
                                if (c.getCompName().equalsIgnoreCase(s)) {
                                    valid = true;
                                    System.out.print("[name|fsurname|ssurname|age|birthday|range|" +
                                            "weight|sensei|gym|country|region]: ");
                                    s = br.readLine();
                                    if (s.equalsIgnoreCase("name")) {
                                        System.out.print("New competitor name?: ");
                                        c.setCompName(br.readLine());
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competitor name sucessfully changed.");
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("fsurname")) {
                                        System.out.print("New competitor first surname?: ");
                                        c.setCompFirstSurname(br.readLine());
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competitor first surname sucessfully changed.");
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("ssurname")) {
                                        System.out.print("New competitor second surname?: ");
                                        c.setCompSecondSurname(br.readLine());
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competitor second surname sucessfully changed.");
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("age")) {
                                        System.out.print("New competitor age?: ");
                                        c.setCompAge(Integer.parseInt(br.readLine()));
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competitor age sucessfully changed.");
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("birthday")) {
                                        System.out.print("New competitor birthday?: ");
                                        String[] bd = br.readLine().split("/");
                                        c.setCompBirthday(Integer.parseInt(bd[2]),
                                                Integer.parseInt(bd[1]),
                                                Integer.parseInt(bd[0]));
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competitor birthday sucessfully changed.");
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("range")) {
                                        System.out.print("New competitor range?: ");
                                        c.setCompRange(CompetitorKarate.getCompRangeGrade(br.readLine()));
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competitor range sucessfully changed.");
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("weight")) {
                                        System.out.print("New competitor weight?: ");
                                        c.setCompWeight(Double.parseDouble(br.readLine()));
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competitor weight sucessfully changed.");
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("sensei")) {
                                        System.out.print("New competitor sensei?: ");
                                        c.setCompSensei(br.readLine());
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competitor sensei sucessfully added.");
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("gym")) {
                                        System.out.print("New competitor gym?: ");
                                        c.setCompGym((br.readLine()));
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competitor gym sucessfully added.");
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("country")) {
                                        System.out.print("New competitor country?: ");
                                        c.setCompCountry((br.readLine()));
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competitor country sucessfully changed.");
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("region")) {
                                        System.out.print("New competitor region?: ");
                                        c.setCompRegion((br.readLine()));
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competitor region sucessfully changed.");
                                        }
                                        break;
                                    } else {
                                        if (logger.isLoggable(Level.WARNING)) {
                                            logger.log(Level.WARNING, "No command found (Level 3a).");
                                        }
                                        break;
                                    }
                                }
                            }
                        } else if (s.equalsIgnoreCase("area")) {
                            System.out.print("Which competition area do you want to edit attributes to? ");
                            s = br.readLine();
                            for (CompetitionAreaKarate ca : competitionArea) {
                                if (ca.getAreaName().equalsIgnoreCase(s)) {
                                    valid = true;
                                    System.out.print("[name|sushin|fukushin|kansa]");
                                    s = br.readLine();
                                    if (s.equalsIgnoreCase("name")) {
                                        System.out.print("New competition area name?: ");
                                        ca.setAreaName(br.readLine());
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competition area name sucessfully changed.");
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("sushin")) {
                                        System.out.print("New competition area sushin name?: ");
                                        ca.setRefereeSushin(br.readLine());
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competition area sushin name sucessfully changed.");
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("fukushin")) {
                                        System.out.print("How many fukushin? (3 or 5): ");
                                        s = br.readLine();
                                        if (s.equalsIgnoreCase("3") || s.equalsIgnoreCase("5")) {
                                            String[] fukushin = new String[Integer.parseInt(s)];
                                            for (int h = 0; h < Integer.parseInt(s); h++) {
                                                System.out.print("Fukushin Name: ");
                                                fukushin[h] = br.readLine();
                                            }
                                            ca.setRefereeFukushin(fukushin);
                                        } else {
                                            if (logger.isLoggable(Level.WARNING)) {
                                                logger.log(Level.WARNING, "No matching fukushin number.");
                                            }
                                        }
                                        break;
                                    } else if (s.equalsIgnoreCase("kansa")) {
                                        System.out.print("New competition area kansa name?: ");
                                        ca.setRefereeKansa(br.readLine());
                                        if (logger.isLoggable(Level.INFO)) {
                                            logger.log(Level.INFO,
                                                    "Competition area kansa name sucessfully changed.");
                                        }
                                        break;
                                    } else {
                                        if (logger.isLoggable(Level.WARNING)) {
                                            logger.log(Level.WARNING, "No command found (Level 3b).");
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                } else if (s.equalsIgnoreCase("print")) {
                    if (logger.isLoggable(Level.INFO)) {
                        logger.log(Level.INFO, "Printing information.");
                    }
                    System.out.println("Competitors: ");
                    for (CompetitorKarate c : competitorList) {
                        System.out.println(c.toString());
                        System.out.println("\n");
                    }
                    System.out.println("******COMPETITORS STATISTICS******");
                    System.out.println("Number of competitors: " + competitorList.size());

                    System.out.println("Categories: ");
                    for (Category cat : categoryList) {
                        System.out.println(cat.toString());
                        System.out.println("\n");
                    }

                    System.out.println("Competition Areas: ");
                    for (CompetitionAreaKarate ca : competitionArea) {
                        System.out.println(ca.toString());
                        System.out.println("\n");
                    }
                } else if (s.equalsIgnoreCase("bye")) {
                    exit = true;
                    if (logger.isLoggable(Level.INFO)) {
                        logger.log(Level.INFO, "Exiting.");
                    }
                } else {
                    if (logger.isLoggable(Level.WARNING)) {
                        logger.log(Level.WARNING, "No command found (Level 1).");
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(TestEntities.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        TestEntities te = new TestEntities();
        te.run();
    }
}
